package com.lanxiao.cloud.modules.oauth.controller;

import com.lanxiao.cloud.common.core.dto.PageQuery;
import com.lanxiao.cloud.common.core.dto.ResultModel;
import com.lanxiao.cloud.common.core.enums.OperationType;
import com.lanxiao.cloud.common.web.invalid.Log;
import com.lanxiao.cloud.modules.oauth.application.RoleCmdManager;
import com.lanxiao.cloud.modules.oauth.application.RoleQueryManager;
import com.lanxiao.cloud.modules.oauth.application.UserRoleContentParse;
import com.lanxiao.cloud.modules.oauth.application.dto.BasicsRoleDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdAdd;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleCmdUpdate;
import com.lanxiao.cloud.modules.oauth.application.dto.RolePageDto;
import com.lanxiao.cloud.modules.oauth.application.dto.RoleQuery;
import com.lanxiao.cloud.modules.oauth.application.dto.UserRoleDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import javax.validation.Valid;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ：qiaoliang
 * @date ：2021-09-16
 */
@Api(tags = "角色")
@Validated
@RequestMapping("/oauths/roles")
@RestController
public class RoleController {

  private final RoleQueryManager queryManager;
  private final RoleCmdManager cmdManager;

  public RoleController(RoleQueryManager queryManager,
      RoleCmdManager cmdManager) {
    this.queryManager = queryManager;
    this.cmdManager = cmdManager;
  }

  @ApiOperation(value = "条件查询角色列表")
  @GetMapping
  public ResultModel<RolePageDto> tree(RoleQuery query, PageQuery pageQuery) {
    return queryManager.page(query, pageQuery);
  }

  @ApiOperation(value = "查询基础角色集合")
  @GetMapping("/basics")
  public List<BasicsRoleDto> query() {
    return queryManager.query();
  }

  @ApiOperation(value = "通过用户ID查询角色列表")
  @GetMapping("/user/{id}")
  public List<UserRoleDto> userRole(@PathVariable Long id) {
    return queryManager.userRoleQuery(id);
  }

  @ApiOperation(value = "新增角色")
  @Log(operationType = OperationType.CREATE)
  @PostMapping
  public ResponseEntity<Void> save(@Valid @RequestBody RoleCmdAdd add) {
    cmdManager.saveRole(add);
    return ResponseEntity.status(HttpStatus.CREATED).build();
  }

  @ApiOperation(value = "修改角色")
  @Log(operationType = OperationType.UPDATE, serviceClass = RoleQueryManager.class)
  @PutMapping("/{id}")
  public ResponseEntity<Void> update(@PathVariable Long id, @Valid @RequestBody RoleCmdUpdate update) {
    cmdManager.updateRole(id, update);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

  @ApiOperation(value = "删除角色")
  @Log(operationType = OperationType.DELETE)
  @DeleteMapping("/{id}")
  public ResponseEntity<Void> delete(@PathVariable Long id) {
    cmdManager.deleteRole(id);
    return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
  }

  @ApiOperation(value = "修改用户角色")
  @Log(operationType = OperationType.UPDATE, parseClass = UserRoleContentParse.class)
  @PutMapping("/user/{id}")
  public ResponseEntity<Void> updateUserAndRole(@PathVariable Long id, @RequestBody List<Long> ids) {
    cmdManager.updateUserAndRole(id, ids);
    return ResponseEntity.status(HttpStatus.PARTIAL_CONTENT).build();
  }

}
